home *** CD-ROM | disk | FTP | other *** search
- Imports System.ServiceProcess
-
- Public Class ServiceControllerForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents txtOut As System.Windows.Forms.TextBox
- Friend WithEvents ServiceController1 As System.ServiceProcess.ServiceController
- Friend WithEvents btnList As System.Windows.Forms.Button
- Friend WithEvents btnQuery As System.Windows.Forms.Button
- Friend WithEvents btnStopIIS As System.Windows.Forms.Button
- Friend WithEvents btnRestartIIS As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnList = New System.Windows.Forms.Button()
- Me.txtOut = New System.Windows.Forms.TextBox()
- Me.ServiceController1 = New System.ServiceProcess.ServiceController()
- Me.btnQuery = New System.Windows.Forms.Button()
- Me.btnStopIIS = New System.Windows.Forms.Button()
- Me.btnRestartIIS = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnList
- '
- Me.btnList.Location = New System.Drawing.Point(8, 16)
- Me.btnList.Name = "btnList"
- Me.btnList.Size = New System.Drawing.Size(120, 40)
- Me.btnList.TabIndex = 1
- Me.btnList.Text = "List services"
- '
- 'txtOut
- '
- Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtOut.Location = New System.Drawing.Point(144, 16)
- Me.txtOut.Multiline = True
- Me.txtOut.Name = "txtOut"
- Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtOut.Size = New System.Drawing.Size(400, 248)
- Me.txtOut.TabIndex = 0
- Me.txtOut.Text = ""
- Me.txtOut.WordWrap = False
- '
- 'ServiceController1
- '
- Me.ServiceController1.MachineName = "francesco"
- Me.ServiceController1.ServiceName = "IISADMIN"
- '
- 'btnQuery
- '
- Me.btnQuery.Location = New System.Drawing.Point(8, 72)
- Me.btnQuery.Name = "btnQuery"
- Me.btnQuery.Size = New System.Drawing.Size(120, 40)
- Me.btnQuery.TabIndex = 1
- Me.btnQuery.Text = "Query IISAdmin status"
- '
- 'btnStopIIS
- '
- Me.btnStopIIS.Location = New System.Drawing.Point(8, 128)
- Me.btnStopIIS.Name = "btnStopIIS"
- Me.btnStopIIS.Size = New System.Drawing.Size(120, 40)
- Me.btnStopIIS.TabIndex = 1
- Me.btnStopIIS.Text = "Stop IISAdmin"
- '
- 'btnRestartIIS
- '
- Me.btnRestartIIS.Location = New System.Drawing.Point(8, 184)
- Me.btnRestartIIS.Name = "btnRestartIIS"
- Me.btnRestartIIS.Size = New System.Drawing.Size(120, 40)
- Me.btnRestartIIS.TabIndex = 1
- Me.btnRestartIIS.Text = "Restart IISAdmin"
- '
- 'ServiceControllerForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(560, 277)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnRestartIIS, Me.btnStopIIS, Me.btnQuery, Me.btnList, Me.txtOut})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "ServiceControllerForm"
- Me.Text = "ServiceController component"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Sub LogMessage(ByVal msg As String)
- txtOut.AppendText(msg & ControlChars.CrLf)
- End Sub
-
- ' list all Windows services
-
- Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
- Dim sc As ServiceController
-
- txtOut.Text = ""
- ' List all non-device services on local machines.
- LogMessage("--- NON-DEVICE SERVICES:")
- For Each sc In ServiceController.GetServices
- LogMessage(sc.ServiceName & " (" & sc.DisplayName & ")")
- Next
-
- ' List all device services on local machines.
- LogMessage("")
- LogMessage("--- DEVICE SERVICES:")
- For Each sc In ServiceController.GetDevices
- LogMessage(sc.ServiceName & " (" & sc.DisplayName & ")")
- Next
- End Sub
-
- Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
- ' display information on the IISADMIN service on the local machine
- Dim scIISAdmin As New ServiceController("IISADMIN", ".")
-
- txtOut.Text = ""
- LogMessage("ServiceName = " & scIISAdmin.ServiceName)
- LogMessage("DisplayName = " & scIISAdmin.DisplayName)
- LogMessage("MachineName = " & scIISAdmin.MachineName)
- LogMessage("Status = " & scIISAdmin.Status.ToString)
- LogMessage("CanStop = " & scIISAdmin.CanStop)
- LogMessage("CanPauseAndContinue = " & scIISAdmin.CanPauseAndContinue)
- LogMessage("CanShutDown = " & scIISAdmin.CanShutdown)
- LogMessage("ServiceType = " & scIISAdmin.ServiceType.ToString)
-
- ' List services on which this depends on.
- LogMessage("Services this service depends on:")
- Dim sc As ServiceController
- For Each sc In scIISAdmin.ServicesDependedOn
- LogMessage(" " & sc.ServiceName & " (" & sc.DisplayName & ")")
- Next
-
- ' List services that depend on this service.
- LogMessage("Services that depend on this service:")
- For Each sc In scIISAdmin.DependentServices
- LogMessage(" " & sc.ServiceName & " (" & sc.DisplayName & ")")
- Next
- End Sub
-
- ' stop the IIS service
-
- Private Sub btnStopIIS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopIIS.Click
- ' Get a reference to the IISADMIN service on the local machine
- Dim scIISAdmin As New ServiceController("IISADMIN", ".")
-
- If scIISAdmin.CanStop Then
- scIISAdmin.Stop()
- ' Wait until the service is stopped.
- scIISAdmin.WaitForStatus(ServiceControllerStatus.Stopped)
- LogMessage("The service has stopped")
- Else
- MessageBox.Show("Unable to stop the service at this time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End If
- End Sub
-
- ' restart the IIS service
-
- Private Sub btnRestartIIS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestartIIS.Click
- ' Get a reference to the IISADMIN service on the local machine
- Dim scIISAdmin As New ServiceController("IISADMIN", ".")
-
- ' Ensure that all the services this service depends on are running.
- Dim sc As ServiceController
- For Each sc In scIISAdmin.ServicesDependedOn
- If sc.Status <> ServiceControllerStatus.Running Then
- sc.Start()
- End If
- Next
-
- ' Now you can start this service.
- scIISAdmin.Start()
-
- ' Wait until the service is running (timeout = 5 secs)
- scIISAdmin.WaitForStatus(ServiceControllerStatus.Running, New TimeSpan(0, 0, 5))
- If scIISAdmin.Status = ServiceControllerStatus.Running Then
- LogMessage("The service is running")
- Else
- LogMessage("Unable to start the service")
- End If
- End Sub
- End Class
-